﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SHKF.B2C.BaseHosted;
using SHKF.B2C.YouZanYun.Model.Dto.ScrmCustomer;
using SHKF.B2C.YouZanYun.Model.Inter.Customer.CreateMember;
using SHKF.B2C.YouZanYun.Model.Inter.Customer.CustomerCreate;
using SHKF.B2C.YouZanYun.Model.Inter.Customer.CustomerPointsIncrease;
using SHKF.B2C.YouZanYun.Model.Inter.Customer.CustomerPointsSync;
using SHKF.B2C.YouZanYun.Model.Inter.Customer.CustomerUpdate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SHKF.B2C.YouZanYun.ServiceS.Controllers
{
    /// <summary>
    /// 会员
    /// </summary>
    [Route("[controller]")]
    [ApiController]
    public class CustomerController : BaseController
    {
        private readonly HttpClientYouZanYun clientYouZanYun;

        public CustomerController(HttpClientYouZanYun clientYouZanYun,
            ILogger<CustomerController> logger) :base(logger)
        {
            this.clientYouZanYun = clientYouZanYun;
        }

        /// <summary>
        /// 会员列表
        /// </summary>
        /// <param name="page_no"></param>
        /// <param name="is_member">-1全部 0表示非会员，1表示会员</param>
        /// <param name="created_at_start"></param>
        /// <param name="created_at_end"></param>
        /// <returns></returns>
        [HttpGet("List")]
        public async Task<ScrmCustomerDto> List(int page_no, int is_member, DateTime created_at_start, DateTime created_at_end)
        {
            ScrmCustomerDto dto = new ScrmCustomerDto();
            try
            { 
                dto = await this.clientYouZanYun.ScrmCustomerList(page_no, is_member, created_at_start, created_at_end); 
            }
            catch (Exception ex)
            {
                base.WriteException("List", ex);
                dto = new ScrmCustomerDto();
                dto.code = -1;
                dto.message = ex.Message; 
            }
            return dto; 
        }

        [HttpPost("Create")]
        public async Task<CustomerCreate3Dto> Create(CustomerCreate3Param data)
        {
            //CustomerCreate3Param data = new CustomerCreate3Param();
            //data.mobile = "13500804714";
            CustomerCreate3Dto dto= await this.clientYouZanYun.ScrmCustomerCreate(data);
            return dto;
        }

        [HttpPost("Update")]
        public async Task<CustomerUpdate3Dto> Update(CustomerUpdate3Param data)
        {
            //更新会员信息

            //CustomerUpdate3Param data = new CustomerUpdate3Param(); 
            //Account account = new Account();
            //account.account_id = "KnFinVgr899733824710201344"; 
            //data.account = account; 
            //customer_update customer_Update = new customer_update();
            //customer_Update.name = "wpwp";
            if(data.customer_update != null)
            {
                data.customer_update.birthday = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            
            CustomerUpdate3Dto dto = await this.clientYouZanYun.ScrmCustomerUpdate(data);
            return dto;
        }

        [HttpPost("CreateMember")]
        public async Task<CustomerUpdate3Dto> CreateMember(CreateMemberParam data)
        {
            //创建会员信息

            //CreateMemberParam data = new CreateMemberParam();
            //data.yz_open_id = "KnFinVgr899733824710201344";

            //member_info member_Info = new member_info();
            //string strDatet = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ms");
            //member_Info.member_created_at = strDatet;// DateTime.Parse(strDatet);
            //data.member_info = member_Info;

            CustomerUpdate3Dto dto = await this.clientYouZanYun.ScrmCustomerCreateMember(data);
            return dto;
        }
         
        [HttpPost("PointsIncrease")]
        public async Task<CustomerPointsDto> PointsIncrease(Params data)
        {
            //增加会员积分

            //CustomerPointsParam data = new CustomerPointsParam();
            //Params paramster =  new Params();
            //paramster.reason = "线下购物";
            //paramster.points = 100;

            //ParamsUser paramsUser = new ParamsUser();
            //paramsUser.account_id = "KnFinVgr899733824710201344"; 
            //paramster.user = paramsUser;

            //data.parameters = paramster;
            CustomerPointsDto dto = new CustomerPointsDto();
            if (data  != null)
            {
                CustomerPointsParam data2 = new CustomerPointsParam();
                Params paramster = new Params();
                paramster.reason = data.reason;
                paramster.points = data.points;
                data2.parameters = paramster;

                ParamsUser paramsUser = new ParamsUser();
                paramsUser.account_id = data.user.account_id;
                paramster.user = paramsUser;

                dto = await this.clientYouZanYun.ScrmCustomerPointsIncrease(data2);
                
            }
            else
            {
                dto = new CustomerPointsDto();
                dto.code = -1;
                dto.message = "数据无空";
            }
            return dto;
        }
         
        [HttpPost("PointsDecrease")]
        public async Task<CustomerPointsDto> PointsDecrease(Params data)
        {
            //减少会员积分

            //CustomerPointsParam data = new CustomerPointsParam();
            //Params paramster = new Params();
            //paramster.reason = "线下购物";
            //paramster.points = 10;

            //ParamsUser paramsUser = new ParamsUser();
            //paramsUser.account_id = "KnFinVgr899733824710201344";
            //paramster.user = paramsUser;

            //data.parameters = paramster;

            CustomerPointsDto dto = new CustomerPointsDto();
            if (data != null)
            {
                CustomerPointsParam data2 = new CustomerPointsParam();
                Params paramster = new Params();
                paramster.reason = data.reason;
                paramster.points = data.points;
                data2.parameters = paramster;

                ParamsUser paramsUser = new ParamsUser();
                paramsUser.account_id = data.user.account_id;
                paramster.user = paramsUser;

                dto = await this.clientYouZanYun.ScrmCustomerPointsDecrease(data2);

            }
            else
            {
                dto = new CustomerPointsDto();
                dto.code = -1;
                dto.message = "数据无空";
            }
            return dto; 
        }

        [HttpPost("PointsSync")]
        public async Task<CustomerPointsDto> PointsSync(CustomerPointsSyncParam data)
        {
            //同步会员积分

            //CustomerPointsSyncParam data = new CustomerPointsSyncParam();
            //data.points = 234;
            //data.reason = "盘点";

            //ParamsUser paramsUser = new ParamsUser();
            //paramsUser.account_id = "KnFinVgr899733824710201344";
            //data.user = paramsUser;
 
            CustomerPointsDto dto = await this.clientYouZanYun.ScrmCustomerPointsSync(data);
            return dto;
        }
    }
}
